
#include "stdafx.h"

#include "HRKExportApp.h"
#include "HRKScene.h"
#include "HRKExport.h"
#include "HRKSceneEnumProc.h"

CHrkSceneEnumProc::CHrkSceneEnumProc(IScene *scene, TimeValue t, Interface *i,BOOL bSelected) 
{
	m_bSelected = bSelected;
	time = t;
	theScene = scene;
	m_nCount = 0;
	this->i = i;
	theScene->EnumTree(this);
}

CHrkSceneEnumProc::~CHrkSceneEnumProc() 
{
	int nCount = m_nodeTab.Count();
	for( int i = 0;i<nCount;i++)
	{
		delete m_nodeTab[i];
	}
	
	m_nCount = 0;	
}

void CHrkSceneEnumProc::Append(INode *node, Object *obj, int type) 
{
	if( m_bSelected )
	{
		if(!node->Selected())
		{
			return ;
		}
	}


	INode* pParent = node->GetParentNode();
	TCHAR* sNameParent =  NULL;

	
	if( pParent )
	{
		sNameParent = pParent->GetName();
		TCHAR* sName =  node->GetName();
		return;
	}
	

	CHrkSceneEntry *entry = new CHrkSceneEntry(node, obj, type);

	m_nodeTab.Append( 1,&entry );
	
	m_nCount++;	
}

Box3 CHrkSceneEnumProc::Bound() 
{
	Box3 bound;
	bound.Init();

	CHrkSceneEntry *e = NULL;
	ViewExp *vpt = i->GetViewport(NULL);

	for( int i = 0;i<m_nCount;i++)
	{
		e = m_nodeTab[i];
		Box3 bb;
		e->obj->GetWorldBoundBox(time, e->node, vpt, bb);
		bound += bb;
		e = e->next;
	}
	
	return bound;
}

int CHrkSceneEnumProc::callback(INode *node) 
{
	// get object from node
	Object *obj = node->EvalWorldState(time).obj;

	// check if it can be represented by a mesh
	if(obj->CanConvertToType(triObjectClassID)) 
	{
		// create a MySceneEntry for the meshable object
		Append(node, obj, OBTYPE_MESH);
	}
	else
	{
		// check for other valid types
  		switch (obj->SuperClassID())
		{ 
			case HELPER_CLASS_ID:
				break;
			case LIGHT_CLASS_ID:
				// check for omni light
				if ( obj->ClassID() == Class_ID(OMNI_LIGHT_CLASS_ID,0 ))
				  Append(node, obj, OBTYPE_OMNILIGHT);
				break;
		}
	} 
	return TREE_CONTINUE;	// Keep on enumeratin'!
}

CHrkSceneEntry *CHrkSceneEnumProc::operator[](int index) 
{
	return m_nodeTab[index];
}



